package com.lineage.server.timecontroller.pc;

import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.PcOtherThreadPool;
import com.lineage.server.world.World;
import java.util.Calendar;
import java.util.Collection;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LawfulTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(LawfulTimer.class);
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 650;
    this._timer = PcOtherThreadPool.get().scheduleAtFixedRate(this, 650L, 650L);
  }
  
  public void run() {
    try {
      Collection<L1PcInstance> all = World.get().getAllPlayers();
      if (all.isEmpty())
        return; 
      Iterator<L1PcInstance> iter = all.iterator();
      while (iter.hasNext()) {
        L1PcInstance tgpc = iter.next();
        if (check(tgpc)) {
          int week = tgpc.getMap().isweek();
          if (tgpc.getMap().isweek() != -1 && tgpc.getMap().isStart_time() > 0 && 
            tgpc.getMap().isEnd_time() > 0) {
            int starttime = tgpc.getMap().isStart_time();
            int unitytime = tgpc.getMap().isEnd_time();
            Calendar date = Calendar.getInstance();
            int nowHour = date.get(11);
            if (starttime >= 0 && unitytime >= 0 && (nowHour < starttime || nowHour >= unitytime)) {
              tgpc.sendPackets((ServerBasePacket)new S_ServerMessage(166, 
                    "★地图限制星期【 " + week + " 】时间【 " + starttime + " 】到【 " + unitytime + "】"));
              L1Teleport.teleport(tgpc, 33440, 32802, (short)4, 0, true);
            } 
          } else if (tgpc.getMap().isStart_time() > 0 && tgpc.getMap().isEnd_time() > 0) {
            int starttime = tgpc.getMap().isStart_time();
            int unitytime = tgpc.getMap().isEnd_time();
            Calendar date = Calendar.getInstance();
            int nowHour = date.get(11);
            if (starttime >= 0 && unitytime >= 0 && (nowHour < starttime || nowHour >= unitytime)) {
              tgpc.sendPackets(
                  (ServerBasePacket)new S_ServerMessage(166, "★地图限制时间【 " + starttime + " 】到【 " + unitytime + "】"));
              L1Teleport.teleport(tgpc, 33440, 32802, (short)4, 0, true);
            } 
          } 
          tgpc.onChangeLawful();
          Thread.sleep(1L);
        } 
      } 
    } catch (Exception e) {
      _log.error("Lawful更新处理时间轴异常重启", e);
      PcOtherThreadPool.get().cancel(this._timer, false);
      LawfulTimer lawfulTimer = new LawfulTimer();
      lawfulTimer.start();
    } 
  }
  
  private static boolean check(L1PcInstance tgpc) {
    try {
      if (tgpc == null)
        return false; 
      if (tgpc.getOnlineStatus() == 0)
        return false; 
      if (tgpc.getNetConnection() == null)
        return false; 
      if (tgpc.isTeleport())
        return false; 
      if (tgpc.isDead())
        return false; 
      if (tgpc.getCurrentHp() <= 0)
        return false; 
    } catch (Exception e) {
      return false;
    } 
    return true;
  }
}
